package com.xt.wikiperfumapi.model.wrapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xt.wikiperfumapi.model.BrandInfo;
import com.xt.wikiperfumapi.model.BrandViewInfo;
import com.xt.wikiperfumapi.model.PerfumeInfo;
import com.xt.wikiperfumapi.model.vo.BrandVo;
import com.xt.wikiperfumapi.model.vo.PerfumeVo;
import com.xt.wikiperfumapi.service.impl.BrandViewInfoServiceImpl;
import com.xt.wikiperfumapi.service.impl.PerfumeInfoServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class BrandWrapper {

    @Resource
    BrandViewInfoServiceImpl brandViewInfoService;

    @Resource
    PerfumeInfoServiceImpl perfumeInfoService;

    @Resource
    PerfumerWrapper perfumerWrapper;
    //品牌及旗下的香水
    public BrandVo brandToVo(BrandInfo brandInfo,String uuid){
        BrandVo brandVo=new BrandVo();
        try {
            BeanUtils.copyProperties(brandVo,brandInfo);
        }catch (Exception e){
            log.info(e.getMessage());
        }
        List<BrandViewInfo> brandViewInfos = brandViewInfoService.list(new QueryWrapper<BrandViewInfo>()
                .eq("brand_id", brandInfo.getBrandId())
        );
        brandVo.setViewCount(brandViewInfos.size());
        List<PerfumeInfo> perfumeInfos = perfumeInfoService.list(new QueryWrapper<PerfumeInfo>()
                .eq("brand_id", brandInfo.getBrandId())
                .eq("is_deleted", 0)
                .orderByDesc("year")
        );
        List<PerfumeVo> perfumeVos=new ArrayList<>();
        perfumeInfos.forEach(el->{
            perfumeVos.add(perfumerWrapper.perfumeToVo(el,uuid));
        });
        brandVo.setPerfumeVos(perfumeVos);
        brandVo.setPerfumeSize(perfumeVos.size());
        return brandVo;
    }

    public BrandVo initDataVo(BrandInfo brandInfo){
        BrandVo brandVo=new BrandVo();
        try {
            BeanUtils.copyProperties(brandVo,brandInfo);
        }catch (Exception e){
            log.info(e.getMessage());
        }
        brandVo.setPerfumeSize((int)perfumeInfoService.count(new QueryWrapper<PerfumeInfo>()
                .eq("brand_id", brandInfo.getBrandId())
                .eq("is_deleted", 0)
                .orderByDesc("year")
        ));
        return brandVo;
    }

}
